package com.itany.corejava.day10;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MyArrayList<E> implements List<E> {

    private Object[] data;
    private int size;

    public MyArrayList() {
        data=new Object[10];
        size=0;
    }

    private void expand(){
        if(size==data.length){
            Object[] temp=new Object[data.length+data.length/2];
            System.arraycopy(data,0,temp,0,data.length);
            data=temp;
        }
    }


    @Override
    public int size() {
        //TODO
        return size;
    }

    @Override
    public boolean isEmpty() {
        //TODO
        return size==0;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        //TODO
        Object[] temp=new Object[size];
        System.arraycopy(data,0,temp,0,size);
        return temp;
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public boolean add(E e) {
        //TODO
        expand();
        data[size++]=e;
        return true;
    }

    @Override
    public boolean remove(Object o) {
        //TODO
        int i;
        if((i=indexOf(o))!=-1){
            remove(i);
            return true;
        }
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {
        //TODO
        size=0;
        data=new Object[10];
    }

    @Override
    public E get(int index) {
        //TODO
        return (E) data[index];
    }

    @Override
    public E set(int index, E element) {
        //TODO
        E temp= (E) data[index];
        data[index]=element;
        return temp;
    }

    @Override
    public void add(int index, E element) {

    }

    @Override
    public E remove(int index) {
        //TODO
        E temp= (E) data[index];

        // for (int i = index; i < size; i++) {
        //     data[i]=data[i+1];
        // }
        System.arraycopy(data,index+1,data,index,size-index-1);
        size--;
        return temp;
    }

    @Override
    public int indexOf(Object o) {
        //TODO
        for (int i = 0; i < size; i++) {
            if((o==null ? get(i)==null : o.equals(get(i)))){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<E> listIterator() {
        return null;
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return null;
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return null;
    }
}
